package cn.edu.scau.cmi.chenbingquan.client;

import java.util.ArrayList;
import cn.edu.scau.cmi.chenbingquan.adapter.SortClassAdapter;
import cn.edu.scau.cmi.chenbingquan.adapter.Student;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;

// 类适配器客户端
public class ClassAdapterClient extends Application {
	private Pane root = new Pane();
	private Button newStudent;
	private Button sort;
	private ListView<Text> lv = new ListView<>();
	private ArrayList<Student> students = new ArrayList<>();
	
	public static void main(String[] args) {
		launch(args);
	}
	
	@Override
	public void start(Stage stage) {
		root.setStyle("-fx-background-color: linear-gradient(to bottom right, ANTIQUEWHITE, CORNFLOWERBLUE)");
		root.setPrefSize(550, 450);
		
		newStudent = new Button("新建学生");
		newStudent.setPrefSize(100, 40);
		newStudent.setLayoutX(60);
		newStudent.setLayoutY(50);
		
		sort = new Button("排序");
		sort.setPrefSize(100, 40);
		sort.setLayoutX(200);
		sort.setLayoutY(50);
		
		lv.getItems().add(new Text("学号" + "\t\t\t" + "名字"));
		lv.setPrefSize(430, 280);
		lv.setFixedCellSize(25);
		lv.setLayoutX(60);
		lv.setLayoutY(150);
		root.getChildren().addAll(newStudent, sort, lv);
		
		Scene scene = new Scene(root);
		stage.setScene(scene);
		stage.getIcons().add(new Image("cn/edu/scau/cmi/chenbingquan/image/窗口左上角图标.png"));
		stage.setTitle("类适配器模式");
		stage.setResizable(false);
		stage.show();
		
		addEvents();
	}

	// 添加事件
	private void addEvents() {
		// 新建学生按钮点击事件
		newStudent.setOnMouseClicked(action -> showNewStudentStage());
		// 排序按钮点击事件	
		sort.setOnMouseClicked(action -> sortAndShow());
	}
	
	// 显示新建学生窗口
	private void showNewStudentStage() {
		Pane pane = new Pane();
		pane.setStyle("-fx-background-color: #44A08D");
					
		TextField tf1 = new TextField();
		tf1.setFocusTraversable(false);
		tf1.setPromptText("请输入学生学号");
		tf1.setPrefHeight(40);
		tf1.setLayoutX(70);
		tf1.setLayoutY(30);
		
		TextField tf2 = new TextField();
		tf2.setFocusTraversable(false);
		tf2.setPromptText("请输入学生名字");
		tf2.setPrefHeight(40);
		tf2.setLayoutX(70);
		tf2.setLayoutY(90);
		
		Button button = new Button("确定");
		button.setPrefSize(60, 30);
		button.setLayoutX(120);
		button.setLayoutY(160);
		pane.getChildren().addAll(tf1, tf2, button);
					
		Stage stage = new Stage();
		Scene scene = new Scene(pane, 300, 250);
		stage.setScene(scene);
		stage.show();
		
		button.setOnMouseClicked(action -> {
			if(tf1.getText().equals("") || tf2.getText().equals("")) {
				showMessageStage("请先输入完整信息!!");
			} else if(isIdExisted(Integer.valueOf(tf1.getText())) == true){
				showMessageStage("该学生已经存在!!");
			} else {
				Student student = new Student(Integer.valueOf(tf1.getText()), tf2.getText());
				students.add(student);
				Text text = new Text(student.getId() + "\t\t\t" + student.getName());
				text.setFill(Color.RED);
				lv.getItems().add(text);
			}
			stage.close();
		});
	}
	
	// 判断指定学号的学生是否存在
	private boolean isIdExisted(int id) {
		for(int i=0; i<students.size(); i++) {
			Student temp = students.get(i);
			if(temp.getId() == id) {
				return true;
			}
		}
		return false;
	}
	
	// 对学生数组排序并显示结果
	private void sortAndShow() {
		SortClassAdapter sca = new SortClassAdapter();
		sca.sort(students);
		
		lv.getItems().clear();
		lv.getItems().add(new Text("学号" + "\t\t\t" + "名字"));
		for(int i=0; i<students.size(); i++) {
			Student temp = students.get(i);
			Text text = new Text(temp.getId() + "\t\t\t" + temp.getName());
			text.setFill(Color.DODGERBLUE);
			lv.getItems().add(text);
		}
	}
	
	// 显示信息窗口
	public void showMessageStage(String message) {
			
		VBox vBox = new VBox(60);
		vBox.setAlignment(Pos.CENTER);
		vBox.setStyle("-fx-background-color: ANTIQUEWHITE");
		vBox.setPrefSize(300, 250);
		
		Text text = new Text(message);
		text.setStyle("-fx-font-size: 25; -fx-fill: orange"); 
			
		Button button = new Button("确定");
		button.setPrefSize(60, 30);
		vBox.getChildren().addAll(text, button);
			
		Stage stage = new Stage();
		Scene scene = new Scene(vBox);
		stage.setScene(scene);
		stage.show();
			
		// 事件
		button.setOnMouseClicked(e -> stage.close() ); 		// 关闭窗口
	}
}
